package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(QueryObject queryObject, Model model) {
        model.addAttribute("result", roleService.query(queryObject));
        return "role/list";
    }

    @RequestMapping("delete")
    public String delete(long id) {
        roleService.delete(id);
        return "redirect:/role/list";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids) {
        roleService.saveOrUpdate(role,ids);
        return "redirect:/role/list";
    }

    @RequestMapping("input")
    public String input(Model model, Long id) {
        if (id != null) {
            model.addAttribute("entity", roleService.get(id));
        }
        model.addAttribute("permissions", permissionService.listAll());
        return "role/input";
    }
}
